To print: Select File and then Print from your browser´s menu.


An intro to Perl

Matt Kynaston introduces the basics of CGI programming and shows you how to put a chat room on your site

What's a camel got to do with it?

If you've been building Web pages for a while, sooner or later you'll come across a site with features you'd dearly love to include in your own. It could be a discussion board, a site-wide search, pages that let users enter personal details that the site stores. No amount of tweaking in your favourite HTML editor can achieve the same effects.

The magic that drives most of these sites are CGI programs. Because CGI programs run on the server, instead of the users browser, they can do almost anything.They're normally found in a directory called the 'cgi-bin', and their power lies in getting information from the user and doing something with it.

Over the next few issues, we're going to open the lid of the cgi-bin, and show you how to make your site respond to users like never before. CGI programming sounds intimidating, but all that bin normally contains are scripts. You should be familiar with HTML before you start, but won't need anything else.

Perl before swine

The vast majority of CGI programs are written in a language called Perl. It started life as a tool to help Unix system administrators carry out routine, boring tasks. But with the advent of the Web, people realised it could be used to create interactive online applications. The back end of the search engine Yahoo was written entirely in Perl - so were tons of other sites.

Unlike languages like C, Perl scripts aren't compiled into some unintelligible binary program. Instead you just upload a text file containing the script, and the Perl interpreter on your host's server takes care of running it.

Click to view full size

Ever wondered what that question mark and other weird stuff is doing in the URL when you're using Yahoo? That's CGI in action

This is important. For one thing, it makes Perl scripts easy to modify and pass around to your friends, which in turn means there's tons of free programs out there that, with a little modification, will do just about anything you could wish for. It also means they can run on any operating system, so long as it has a Perl interpreter installed. And the Perl interpreters are free too. We like free.

We're going to show you how to use Perl scripts on your own site and give you just enough knowledge of the language so you can customise them if you need to. But this isn't going to be a tutorial on programming in Perl. If you like what you see and want to start bashing out code yourself, there's some excellent books that'll help (see the Resources box) and a whole online community of 'Perl Mongers' waiting to welcome you into their (sometimes strange) world.

There's another reason for only scratching at Perl. Although it's powerful, well respected, and an excellent thing to have a nodding acquaintance with, it wasn't originally built for Web design. In recent years, a whole bunch of other languages have been created just to handle the task of interactive Web design, including Microsoft's Active Server Pages (ASP), Java's JSP and a completely Open Source language called PHP. They're much easier than Perl to learn and use, so we'll devote the third and fourth part of this series to them.

Serving yourself

Enough background. Onto some Perl hacking. All you really need to get your scripts working is good old Notepad (or another text editor) and some Web space with a CGI bin. You've got almost certainly Notepad already, and you'll find a list of completely free hosts in the Resources section.

But it's useful to try out your Perl scripts on your own computer before you upload them. It'll save you the connection charges while you figure things out, and makes ironing out bugs much easier. For this you'll need a Web server and a Perl interpreter.

Click to view full size

Check you've got a green Xitami icon at the bottom right of your screen. If it's red, the server isn't running.

Running a Web server sounds like a big deal, and possibly expensive. It isn't. Microsoft used to make a program called Personal Web Server (PWS) that ran on Windows 95 and 98, but unfortunately it no longer supported in Windows Millennium and is tricky to set up to run Perl. But there are other, better servers around. We've settled on Xitami, because it works straight out the box, is easy to use and (is this beginning to sound familiar?) completely free.

We've bundled Xitami together with ActivePerl - the version of Perl that runs under Windows - into a single set up program on the CD. Fire it up before going further. If you're already using PWS, the installer will give you the choice of using it instead of Xitami. Please read the Using Personal Web Server box before deciding which to use.

Testing Perl

Once the setup is finished and your computer has restarted, it's time to try out your first script. When you're running a Web server on your own computer, your machine becomes the 'localhost'. To view Web pages from the server, open your browser and type http://127.0.0.1 into the address bar. This number is a special IP address that always points to the local server. Most computers will also understand the easier to remember localhost address if you type it into your browser.

Click to view full size

If you get a 'Web page unavailable offline' message when you try to access your server, don't panic.Click 'Connect' - your modem won't dial.

Try it. You'll get a screen saying 'Welcome to Xitami' with lots of information about the program and links to it's documentation. Now make sure Perl is working properly. Type http://127.0.0.1/cgi-bin/test-perl.pl into the address bar. If all's well, you'll get a message saying 'Hello from Perl' and giving you some information about the server environment.

If you run into errors at this point, check Xitami is actually running. If it still doesn't work, the chances are the set up didn't go smoothly. Make sure you're not running another Web server. If not, use Add/Remove Programs in your Control Panel to uninstall Xitami and ActivePerl and run the installer again, making sure you restart your computer when prompted.

Getting chatty

Now it's time to get Perl doing something useful. We're going to add a simple chat room to our Web site, using a free program called EveryChat. If you've got last month's CD, you'll find it there, or you can download it from www.everysoft.com/everychat. Copy the Zip file to your hard drive and extract the files using WinZip or a similar zip program.

Now take a look in the folder you installed Xitami to - it'll normally be C:\Xitami. You'll notice a bunch of folders inside it. The two important ones are the cgi-bin folder and the webpages folder. Copy the evercht.cgi file from zip to your cgi-bin, then create a new folder inside webpages. Call it chat, and make another folder inside it called messages.

Copy chatform.html, chatframes.html and chattop.html to the chat folder, then copy messages.html to the messages subfolder. Open chatforms.html in Notepad and find the '<FORM ACTION=..' line. Change the address to '/cgi-bin/everycht.cgi'. Do the same to chatframes.html.

When you give a URL starting with a forward slash, the Web server starts at the 'top' of your Web site (the root), and works down from there. In Xitami, this root is the webpages directory. But the cgi-bin is a bit special. Even though it's above the root on your hard drive, the server creates a 'virtual' directory inside the root. This is done for security reasons.

Now open everycht.cgi with Notepad. Be careful about using other text editors with Perl scripts - some, such as Word, leave garbage at the ends of the lines that will stop them being read correctly. Notepad might not be pretty, but it doesn't mess with things.

Scroll down until you get to the section marked 'CONFIGURATION'. You'll see an indented line starting '$filepath='. Replace the text between the quotes with full path to your new messages directory from the root of your drive - it should be something like: '/Xitami/webpages/chat/messages/'. Notice we've used forward slashes all the way through, and added one to the end.

Because Perl programs run on the server directly, instead of through a Web server like Xitami, their 'root' is at the top of your host's directory structure, not the Web server's. It's important to remember this when it comes time to upload your CGI programs. The actual location of your cgi-bin directory will vary depending on how your host has their server configured. Check their help pages or use the test-perl.pl script, as described in Uploading your Chat Room, and change the $filepath line accordingly.

Click to view full size

If you ever need to change the directories Xitami stores your Web site in, take a look at the setup pages at http://127.0.0.1/admin

Getting it to work

Now point your browser at http://127.0.0.1/chat/chatframes.html. Give yourself a name and click Submit Name button. Once the centre screen refreshes, you'll see your name enter the chat room, and you're away.

The script doesn't produce the prettiest chat room going, but there's lots of ways you can customise it before letting it lose on the world. Tale a look through the Readme.txt file that came with EveryChat for some ideas. We're going plough on and upload it, however. See Uploading your Chat Room to learn how it's done.

Once you've got the basics down, check out the other sections in this article: Bughunting - for when things go wrong, Smartass - for the lowdown on CGI jargon and Resources - for links to great Perl sites.

Next month, we'll go into more detail about how Perl scripts are put together so you can start getting your hands dirty in the code. Until then, happy Perl Mongering.



Uploading your CGI

 

Click to view full size

One

Set up an account with a host that gives you cgi-bin access and get your hands on an FTP client, such as CuteFTP or LeechFTP. Make sure the transfer type is set to ASCII or Text - or your Perl program will not work. Then upload test-perl.pl to your cgi-bin.

Two

If you're using a Unix type host, such as www.uklinux.net, you must set the file permissions on any script you upload to 755. Right click on the file and select CHMOD (CuteFTP) or Set Attributes (LeechFTP). Check the cgi-bin directory itself has the same permissions.

Click to view full size

 

 

Click to view full size

Three

Point your browser at your script - something like www.you.yourhost.net/cgi-bin/test-perl.pl. Scroll down the list of environment variables until you get to one like SCRIPT_FILENAME. This should show you the absolute path to your Web directory and cgi-bin.

Four

If you get an error 500 or an 'Internal Server Error, you probably didn't set the transfer type to ASCII. Delete the file and upload it again, transferring it as ASCII. Then check you've got the permissions right. The magic 755 tells the server it's an executable script.

Click to view full size

 

 

Click to view full size

Five

Now create directories for your Web pages exactly as you had them on your hard drive. Right-click your messages directory and change its permission to 777 - this tells the server that anyone can read, write or change into it.

Six

Upload chatform.html and chatframes.html to your chat directory, then upload messages.html to the messages subdirectory. Right-click on messages.html and change its permissions to 666 - this ensures the EveryChat can update the file with new messages.

Click to view full size

 

 

Click to view full size

Seven

Now edit everycht.cgi. Look for the $filepath='…' line in the configuration part, and change it to the absolute path to your messages directory. It should have a forward slash at the beginning and end, something like '/www/me.myhost.net/chat/messages/'

Eight

Save your work and upload it to your cgi-bin. Change it's permissions to 755 so it's executable, then point your browser at chatframes.html. If all's gone well, you'll be able to log on and start chatting right away. Congratulations!

Click to view full size

 


Debugging Perl

Broken your script? Try some of these tricks to find out what's wrong

 

Click to view full size

One

The error logs your server generates may contain some useful information. In Xitami, you'll find them in C:\Xitami\cgierr.log. Open it in a text editor and you should get some idea where the problem lies

Two

Your host may give you access to their logs, too. The dreaded 500 server error almost always comes from either the file permissions being set wrong or not uploading your script in ASCII mode. Delete your script and upload it again.

Click to view full size

 

 

Click to view full size

Three

Test your script from the command line. Open an MS-DOS Prompt window type 'CD \Xitami\cgi-bin'. Then type 'perl -w' followed by the name of your script. You should see the all the HTML it would normally send to your browser spew across your screen.

Four

The '-w' switch turns warnings on. Not every warning is bad - if you see lines about 'uninitialized values', don't worry - they're not fatal. But look for the line numbers of anything more dire, then go back to the code and fix them.

Click to view full size

 


Personal Web Server and Perl

Personal Web Server comes with Microsoft's Frontpage, and if you're using Windows 98, you can install it straight from your Windows CD. But it's not compatible with Windows Me, and is requires some tweaking before it will happily run Perl scripts. We don't recommend using it for these tutorials, but running two Web servers on one machine can cause problems.

If you're hooked on Frontpage and don't want to uninstall PWS, our setup program will give you the choice of letting it make some changes to your registry that will enable Perl to work on PWS. But, unlike Xitami, PWS won't automatically recognise Perl scripts for what they are. Instead, it relies on the file extension of the script. These must be either '.pl' or '.cgi'.

Many of the scripts you download won't use these extensions, so you'll have to change not only the file name, but any references to the files in your HTML and even within the Perl script itself. Also be sure to consult both ActivePerl's documentation and PWS's online help for instructions on setting up your CGI bin.


Smartass

There's nothing like a bunch of jargon to make you feel dumb. Here's what it all means...

CGI - Common Gateway Interface. The standard way for a Web server to send and receive data to and from the user

cgi-bin - the most common name for the directory on your Web server where CGI scripts are uploaded to.

chmod - a Unix program that changes the permissions on files. See 'file permissions'.

file permissions - Unix files and directories are given permissions to say who can read and write to them, as well as if they can be executed. Perl scripts and cgi-bin directories should have their permissions set to 755, which indicates anyone can read and execute them, but only their owner can write to them.

Host - a company that provides space on their Web server. If you're using CGI and Perl, you must make sure your host is set up to let you run your scripts on their server. See the Resources section

Open Source - a licensing model that states that the source code of the software must be freely available, enabling anyone to modify it and contribute to the software's development. See www.opensource.org

Perl - Practical Extraction and Reporting Language (or Pathologically Eclectic Rubbish Lister). A scripting language created by Larry Wall to handle Unix system administration tasks. Rapidly became a popular CGI programming language, and has been referred to as 'the glue of the Internet'

Web server - a computer that sends Web pages to a user's browser. There are many different Web server programs available, including IIS for Windows 2000, Apache and Xitami


Perl resources

We've only scraped the surface of the wild world of Perl. Here's where to find out more...

Perl

www.activestate.com
Home of ActivePerl, the Perl interpreter for Windows. Go here for updated documentation, development kits and more.

www.perl.com
The official home of the Perl language with O'Reilly Publishing. Docs, FAQs and news.

www.findtutorials.com
Want to track down a tutorial on Perl? Start here.

www.netmag.co.uk/forums/forum_web_builder.htm
Got a question? Ask it on .net's Web builder forum.

Free hosting with CGI

www.uklinux.net
Our fave: 20MB Web space for free, with Perl, CGI, PHP good help pages, no ads, and their profits go to developing Open Source software

Other free hosts with CGI - some may place banner ad on your site.
www.portland.co.uk
www.virtualave.net
free.prohosting.com
www.tripod.com
www.netfirms.com

Free scripts

www.perlarchive.com
Thousands of Perl scripts, documentation and an excellent online forum for asking questions

www.cgi-resources.com
Huge archive of scripts to do almost anything you could want, plus links to tutorials and documentation

www.bignosebird.com
OK, not quite so many scripts here, but we like the site name

www.scriptsearch.com
Want to find a script quickly? Try this script search engine

Books


Learning Perl, 2nd Edition, Schwartz & Christiansen, O'Reilly, 1997.
So you want to get to grips with this whole Perl thing? Learning Perl has been the start for many a PerlMonger.

Learning Perl on Win32 Systems, Swartz, Olson & Christiansen, O'Reilly, 1997.
An unfortunately slightly out-of-date book, but a good introduction to Perl for Windows users.

Perl and CGI for the World Wide Web, Elizabeth Castro, Addison-Wesley 1998.
A nice, visual guide to getting started with Perl on your Web site.

 

This article was first published in .net magazine, issue 81. You can subscribe to .net here or order back issues from backissues@futurenet.co.uk.

 


This article originally appeared in issue 81 of .net magazine.
All rights reserved. © Future Publishing 2000.